package com.ceba.test.base.utils;

import com.ceba.base.helper.LoggerHelper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

public class CSVUtils {

    /**
     * CSV文件生成方法
     * @param head
     * @param dataList
     * @param outPutPath
     * @param filename
     * @return
     */
    public static File createCSVFile(List<Object> head, List<List<Object>> dataList,
                                     String outPutPath, String filename) {

        File csvFile = null;
        BufferedWriter csvWtriter = null;
        try {
            csvFile = new File(outPutPath + File.separator + filename + ".csv");
            File parent = csvFile.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
            csvFile.createNewFile();

            // GB2312使正确读取分隔符","
            csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                    csvFile), "GB2312"), 1024);
            // 写入文件头部
            writeRow(head, csvWtriter);

            // 写入文件内容
            for (List<Object> row : dataList) {
                writeRow(row, csvWtriter);
            }
            csvWtriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                csvWtriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return csvFile;
    }

    /**
     * 写一行数据方法
     * @param row
     * @param csvWriter
     * @throws IOException
     */
    private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
        // 写入文件头部
        for (Object data : row) {
            StringBuffer sb = new StringBuffer();
            String rowStr = sb.append("\"").append(data).append("\",").toString();
            csvWriter.write(rowStr);
        }
        csvWriter.newLine();
    }

    public static void writeCSV(String title,List<List<Object>> dataList, String finalPath) {
        FileOutputStream out = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            File finalCSVFile = new File(finalPath);
            out = new FileOutputStream(finalCSVFile);
            osw = new OutputStreamWriter(out, "GBK");
            // 手动加上BOM标识
//			osw.write(new String(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}));
            bw = new BufferedWriter(osw);
            /**
             * 往CSV中写新数据
             */
            String gbkTitle = new String(title.getBytes(),"GBK");
            bw.append(gbkTitle).append("\r");
            if (dataList != null && !dataList.isEmpty()) {
                for(List<Object> row : dataList){
                    StringBuffer sb = new StringBuffer();
                    String rowStr = "";
                    for(Object object : row){
                        rowStr = sb.append("\"").append(object).append("\",").toString();
                    }
                    bw.append(rowStr).append("\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if (bw != null) {
                    bw.close();
                    bw = null;
                }
                if (osw != null) {
                    osw.close();
                    osw = null;
                }
                if (out != null) {
                    out.close();
                    out = null;
                }
            }catch (Exception e){
                LoggerHelper.error(e,"");
            }
        }
        System.out.println(finalPath + "数据导出成功");
    }
}
